package com.power.sifa.approve.dao.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.plf.core.common.dao.OffersetTransformers;
import com.power.sifa.approve.dao.Apl_notepadDaoCustom;
import com.power.sifa.approve.model.Apl_notepad;
import com.power.sifa.approve.model.pageInfo.Apl_notepadPageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.internal.NativeQueryImpl;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* 行政复议记事本信息 持久层实现(hibernate)
*/
@Slf4j
@Repository
public class Apl_notepadDaoImpl implements Apl_notepadDaoCustom{
    private final EntityManager entityManager;
    public Apl_notepadDaoImpl(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    /**
     * 行政复议 记事本 分页查询
     * @param pageInfo
     * @return
     */
    @Override
    public List<Apl_notepad> findAll(Apl_notepadPageInfo pageInfo) {
        List<Apl_notepad> result = new ArrayList<>();
        StringBuffer sql  = new StringBuffer(" select S.*, w.applyname, w.targetname from ( SELECT A .ID AS ID, b.acceptcode AS acceptcode, A.applyid, b.status, b.instid AS instid, A.createdate FROM ( SELECT * FROM ( SELECT T.*, ROW_NUMBER ( ) OVER ( PARTITION BY T.applyid ORDER BY createdate DESC ) rk FROM apl_notepad T ) WHERE rk = 1 ) A LEFT JOIN apl_main b ON A.applyid = b.ID ) S join apl_main T on S.acceptcode = T.acceptcode and T.applycode is not null join apl_admreview W on T.id = W.id where 1=1 ");
        List<Object> paramsList = new ArrayList<>();
        int index = 1;

        // 案号查询
        if (StringUtils.isNotEmpty(pageInfo.getF_acceptcode())) {
            sql.append(" and S.acceptcode like ?" + index++);
            paramsList.add("%" + pageInfo.getF_acceptcode()+ "%");
        }

        //申请人
        if(StringUtils.isNotEmpty(pageInfo.getF_applyname())){
            sql.append(" and w.applyname like ?" + index++);
            paramsList.add("%" + pageInfo.getF_applyname()+ "%");
        }

        //被申请人
        if(StringUtils.isNotEmpty(pageInfo.getF_targetname())){
            sql.append(" and w.targetname like ?" + index++);
            paramsList.add("%" + pageInfo.getF_targetname()+ "%");
        }

        //排序
        sql.append(" order by createdate desc, " + pageInfo.getSort() + " " + pageInfo.getOrder());
        Query query = entityManager.createNativeQuery(sql.toString());
        query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);
        log.info(sql.toString());


        // 添加条件
        for (int i = 0; i <= paramsList.size() - 1; i++) {
            query.setParameter(i + 1, paramsList.get(i));
        }
        // 查询总数
        pageInfo.setAllcount(query.getResultList().size());
        // 分页
        if (pageInfo.getAllcount() > 0) {
            int pageStart = (pageInfo.getPageNumber() - 1) * pageInfo.getPageSize();
            List<Map> list = query.setFirstResult(pageStart).setMaxResults(pageInfo.getPageSize()).getResultList();
            if (CollectionUtil.isNotEmpty(list)) {
                result = JSON.parseArray(JSON.toJSONString(list), Apl_notepad.class);
            }
        }
        return result;
    }


}